<?php if (!defined('BASEPATH'))exit('No direct script access allowed');
/**
 * 新手任务模块控制器
 */
class Mtask extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('new_task_model');
        $this->load->model('account_model');
        $this->load->model('users_log_model');
        $this->load->model('finance_list_model');
        $this->load->model('experience_list_model');
        $this->new_task = $this->config_task['new_task'];
    }
    //新手任务首页
    function index() {
        if($this->userinfo){
            $tasks = $this->new_task_model->get_task($this->userinfo['user_id']);
            $this->assign('step_one', $tasks[0]);
            $this->assign('step_two', $tasks[1]);
            $this->assign('step_three', $tasks[2]); 
            $this->assign('step_four', $tasks[3]); 
        }
        $this->assign("new_task", $this->new_task);
        $this->display('mtask/index.html');
    }
    
    //完成任务，获取奖励
    function donenewtask(){
        $mess = array();
        if($_POST){
            
            if(!$this->userinfo){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1001;
                $mess['errorMsg'] = "访问超时，请重新登录";
                echo JSON($mess);exit;
            }
            $step = trim($_POST['step']);
            $task = $this->new_task[$step-1];
            if($step != 4){
                //获取我的任务
                $where = array();
                $where['user_id'] = $this->userinfo['user_id'];
                $where['step'] = $step;
                $mytask = $this->new_task_model->get($where);
                if(!$mytask){
                    $mess['reqStatus'] = false;
                    $mess['errorCode'] = 1000;
                    $mess['errorMsg'] = "抱歉，找不到该任务"; 
                    echo JSON($mess);exit;
                }
                if($mytask['receive_time']){
                    $mess['reqStatus'] = false;
                    $mess['errorCode'] = 1000;
                    $mess['errorMsg'] = "已经领取过奖励，不能贪心哦";            
                    echo JSON($mess);exit;
                }
            }
            $step_title = $step ;
            $finance_award = $task['award_finance'];//奖励溜溜币
            $experience_award = $task['award_experience'];//奖励积分
            //更新任务
            if($step == 4){
                $new_user_task4 = array();
                $new_user_task4['user_id'] = $this->userinfo['user_id'];
                $new_user_task4['step'] = 4;
                $new_user_task4['reward_worth'] = $this->new_task[3]['award_finance'];
                $new_user_task4['reward_experience'] = $this->new_task[3]['award_experience'];
                $new_user_task4['is_finish'] = 1;
                $new_user_task4['is_receive'] = 1;
                $new_user_task4['receive_time'] = time();
                $new_user_task4['finish_time'] = time();
                $new_user_task4['create_time'] = time();
                $this->new_task_model->insert($new_user_task4);//新手任务第一步
            }else{
                 $task_data = array();
                $task_data['is_receive'] = 1;
                $task_data['receive_time'] = time();
                $result = $this->new_task_model->update($task_data,array('id'=>$mytask['id']));
            }
            if($step == 4){
                //更新用户
                $this->users_model->user_update(array('is_new'=>1),$this->userinfo['user_id']);
            }
            if($step == 4 && $this->user_info['parent_id']){
                $this_time = time();
                $spread_award_finance = 10000;//好友邀请奖励溜溜币
                $spread_award_experience = 100;//好友邀请奖励积分
                //获取账号信息
                $account_parent = $this->account_model->get($this->user_info['parent_id']);
                $account_parent_data = array();
                $account_parent_data['total'] = $account_parent['total'] + $spread_award_finance;//账户总额
                $account_parent_data['balance'] = $account_parent['balance'] + $spread_award_finance;//账户余额
//                $account_parent_data['experience'] = $account_parent['experience'] + $spread_award_experience;//当前积分值
//                $account_parent_data['experience_balance'] = $account_parent['experience_balance'] +  $spread_award_experience;//可用积分值
                //更新账户
                $this->account_model->update($account_parent_data,$this->user_info['parent_id']);
                //写入溜溜币明细
                $finance_list_data = array();
                $finance_list_data['user_id'] = $this->user_info['parent_id'];
                $finance_list_data['content'] = '好友【'.$this->user_info['username'].'】完成新手任务，奖励'.$spread_award_finance.'溜溜币';
                $finance_list_data['finance'] = $spread_award_finance;
                $finance_list_data['finance_balance'] = $account_parent_data['balance'];
                $finance_list_data['reward_type'] = 'spread_award';
                $finance_list_data['type'] = 1;
                $finance_list_data['create_time'] = $this_time;
                $finance_list_data['month'] = date("Ym");
                $finance_list_data['day'] = date("Ymd");
                $finance_list_data['friend_id'] = $this->userinfo['user_id'];
                $finance_list_data['friend_flag'] = 1;
                $this->finance_list_model->insert($finance_list_data);
//                //写入积分明细
//                $experience_list_data = array();
//                $experience_list_data['user_id'] = $this->user_info['parent_id'];
//                $experience_list_data['content'] ='好友【'.$this->user_info['username'].'】完成新手任务，奖励'.$spread_award_experience.'积分';
//                $experience_list_data['experience'] = $spread_award_experience;
//                $experience_list_data['experience_balance'] = $account_parent_data['experience_balance'];
//                $experience_list_data['reward_type'] = 'spread_award';
//                $experience_list_data['type'] = 1;
//                $experience_list_data['create_time'] = $this_time;
//                $experience_list_data['month'] = date("Ym");
//                $experience_list_data['day'] = date("Ymd");
//                $experience_list_data['friend_id'] = $this->userinfo['user_id'];
//                $experience_list_data['friend_flag'] = 1;
//                $this->experience_list_model->insert($experience_list_data);

                $message = "您的好友【".name_formart($this->user_info['username'])."】完成新手任务,奖励您".$spread_award_finance."溜溜币，请注意查收。感谢您对弹溜溜的支持！";//和".$spread_award_experience."积分
                $message = mail_notice_tpl($this->config_tpl['mail_tpl'], $message);
                $this->db->insert("users_award_notice", array('user_id'=>$this->user_info['parent_id'],'title'=>'邀请好友奖励通知','content'=>$message,'from'=>'弹溜溜官方','create_time'=>  time()));

            }

            //获取账号信息
            $account = $this->account_model->get($this->userinfo['user_id']);
            $account_data = array();
            $account_data['total'] = $account['total'] + $finance_award;//账户总额
            $account_data['balance'] = $account['balance'] + $finance_award;//账户余额
//            $account_data['experience'] = $account['experience'] + $experience_award;//当前积分值
//            $account_data['experience_balance'] = $account['experience_balance'] +  $experience_award;//可用积分值
            //更新账户
            $this->account_model->update($account_data,$this->userinfo['user_id']);
            //写入溜溜币明细
            $finance_list_data = array();
            $finance_list_data['user_id'] = $this->userinfo['user_id'];
            $finance_list_data['content'] = $task['title'].'，奖励'.$finance_award.'溜溜币';
            $finance_list_data['finance'] = $finance_award;
            $finance_list_data['finance_balance'] = $account_data['balance'];
            $finance_list_data['reward_type'] = 'task_award';
            $finance_list_data['type'] = 1;
            $finance_list_data['create_time'] = time();
            $finance_list_data['month'] = date("Ym");
            $finance_list_data['day'] = date("Ymd");
            $this->finance_list_model->insert($finance_list_data);
            //写入积分明细
//            $experience_list_data = array();
//            $experience_list_data['user_id'] = $this->userinfo['user_id'];
//            $experience_list_data['content'] = $task['title'].'，奖励'.$experience_award.'积分';;
//            $experience_list_data['experience'] = $experience_award;
//            $experience_list_data['experience_balance'] = $account_data['experience_balance'];
//            $experience_list_data['reward_type'] = 'task_award';
//            $experience_list_data['type'] = 1;
//            $experience_list_data['create_time'] = time();
//            $experience_list_data['month'] = date("Ym");
//            $experience_list_data['day'] = date("Ymd");
//            $this->experience_list_model->insert($experience_list_data);

            //日志
            $log_data = array();
            $log_data['user_id'] = $this->userinfo['user_id'];
            $log_data['events'] = 'sign';
            $log_data['content'] = '任务奖励成功';
            $log_data['ip'] = $this->input->ip_address();
            $log_data['create_time'] = time();
            $this->users_log_model->insert($log_data);

            $mess['step_title'] = $step_title;
            $mess['icon'] = $finance_award;
            $mess['expe'] = 0;//$experience_award
            $mess['reqStatus'] = true;
            echo JSON($mess);exit;
        }else{
            $mess['reqStatus'] = false;
            $mess['errorCode'] = 1004;
            $mess['errorMsg'] = "非法访问";
            echo JSON($mess);exit; 
        }
    }
    
}
